
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <wchar.h>
#include <sys/ioctl.h>


#include "font_manager.h"
#include <ft2build.h>

#include FT_FREETYPE_H
#include FT_GLYPH_H





FT_Face 	  g_face;
static int g_defaultfontsize=12;

static int FreetypeFontInit(char * finename)
{
	FT_Library	  library;
	int error;
	
	error = FT_Init_FreeType( &library );			   /* initialize library */
	if(error)
	{
		printf("Freetype init error\n");
		return -1;

	}
	error = FT_New_Face( library, finename, 0, &g_face ); /* create face object */
	if(error)
	{
		printf("Freetype New_Face error\n");
		return -1;

	}
	


//默认字体大小
	FT_Set_Pixel_Sizes(g_face, g_defaultfontsize, 0);
	FT_Set_Pixel_Sizes(g_face, 100, 0);

	printf("%s %s %d %p\n", __FILE__, __FUNCTION__, __LINE__, g_face);

	return 0;



}
static int FreetypeSetFontSize(int fontsize)
{
	int err;

	printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);


	err =FT_Set_Pixel_Sizes(g_face, fontsize, 0);


		printf("%s %s %d %d\n", __FILE__, __FUNCTION__, __LINE__,err);

	return 0;

}



static int FreetypeGetFontBitMap(unsigned  int dwcode, fontbitmap_p fontbitmapp)
{ 
	int error;

	FT_Vector pen;
//    FT_Glyph  glyph;
    FT_GlyphSlot slot = g_face->glyph;

    pen.x = fontbitmapp->CurOrigin_x  *64; /* 单位: 1/64像素 */
    pen.y = fontbitmapp->CurOrigin_y  *64; /* 单位: 1/64像素 */


	FT_Set_Transform(g_face, 0, &pen);


	/* 加载位图: load glyph image into the slot (erase previous one) */
	error = FT_Load_Char(g_face, dwcode, FT_LOAD_RENDER);
	if(error)
	{	
		printf("FT_Load_Char error \n");
		return -1;

	}

	fontbitmapp->bitmapbuff = slot->bitmap.buffer;
	fontbitmapp->tRegion.leftup_x   = slot ->bitmap_left;
	//笛卡尔坐标
	fontbitmapp->tRegion.leftup_y   = fontbitmapp->CurOrigin_y*2 - slot ->bitmap_top;
	fontbitmapp->tRegion.width      = slot ->bitmap.width;
	fontbitmapp->tRegion.heigh      = slot ->bitmap.rows;
	fontbitmapp->NexOrigin_x= fontbitmapp->CurOrigin_x + slot->advance.x / 64;
	fontbitmapp->NexOrigin_y= fontbitmapp->CurOrigin_y ;


return 0;
//	  draw_bitmap( &slot->bitmap,slot->bitmap_left,var.yres - slot->bitmap_top);


}

static fontoperation freetype_operation={

	.name="freetype",
	.FontInit=FreetypeFontInit,
	.SetFontSize=FreetypeSetFontSize,
	.GetFontBitMap=FreetypeGetFontBitMap,
};


 void freetypeRegsiter(void)
{
	


	RegsiterFontlib(&freetype_operation);

	



}











